//
//  ImageTittledButton.m
//  2bulu-QuanZi
//
//  Created by 罗亮富 on 14-8-6.
//  Copyright (c) 2014年 Lolaage. All rights reserved.
//

#import "ImageTittledButton.h"
#import "UIView+Ulti.h"


@implementation ImageTittledButton

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initialParameters];
    }
    return self;
}

-(void)awakeFromNib
{
    [super awakeFromNib];
    [self initialParameters];
}

-(void)initialParameters
{
    self.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
    self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.font = [UIFont systemFontOfSize:13.0];
    [self setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
    self.titleEdgeInsets = UIEdgeInsetsMake(2,1,2,1);
    _alignStyle = ImageTiltleButonStyleVertical;
    _contentGap = 3.0;
}

-(void)layoutSubviews
{
    [super layoutSubviews];

    
    CGRect titleLabelFrame = self.titleLabel.frame;
    CGSize labelSize = [self.titleLabel.text boundingRectWithSize:CGSizeMake(self.frame.size.width, CGFLOAT_MAX)
                                                          options:(NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin)
                                                       attributes:@{NSFontAttributeName:self.titleLabel.font} context:nil].size;
    
    
    CGSize imageSize = self.imageView.image.size;
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    
    CGFloat imageY = self.titleEdgeInsets.top;
    CGFloat imageX=self.titleEdgeInsets.left,imageH=0,imageW=0;
    if(self.alignStyle == ImageTiltleButonStyleVertical)
    {
        titleLabelFrame.size.width = MIN(self.width-self.titleEdgeInsets.left-self.titleEdgeInsets.right, labelSize.width);
        titleLabelFrame.size.height = labelSize.height;
        titleLabelFrame.origin.x = (self.frame.size.width / 2) - (labelSize.width / 2);
        titleLabelFrame.origin.y = self.frame.size.height-self.titleEdgeInsets.bottom-labelSize.height;
        
        
        
        imageH = titleLabelFrame.origin.y - imageY - _contentGap;
        imageW = MIN(imageSize.width/imageSize.height * imageH, self.width-self.titleEdgeInsets.left-self.titleEdgeInsets.right);
        imageX = (self.frame.size.width-imageW)/2;
    }
    else
    {
        imageH = self.frame.size.height - self.titleEdgeInsets.top - self.titleEdgeInsets.bottom;
        imageW = (imageSize.width/imageSize.height)*imageH;
        
        titleLabelFrame.origin.x = imageX+imageW+_contentGap;
        titleLabelFrame.size.width = MIN(self.frame.size.width-self.titleEdgeInsets.left-titleLabelFrame.origin.x,labelSize.width);
        titleLabelFrame.size.height = labelSize.height;
        titleLabelFrame.origin.y = imageY+(imageH-titleLabelFrame.size.height)/2;//MAX((self.frame.size.height-titleLabelFrame.size.height)/2,self.titleEdgeInsets.top);
        
        CGFloat shift = titleLabelFrame.origin.x+titleLabelFrame.size.width - self.frame.size.width-self.titleEdgeInsets.right;
        if(shift < 0)
        {
            imageX += -0.5*shift;
            titleLabelFrame.origin.x += -0.5*shift;
        }
        
    }
    
    self.titleLabel.frame = titleLabelFrame;
    self.imageView.frame = CGRectMake(imageX,imageY, imageW, imageH);
    
}

-(void)sizeToFit
{
    [super sizeToFit];
    CGPoint center = self.center;
    CGFloat w1 = self.imageView.image.size.width;
    CGFloat w2 = self.titleLabel.frame.size.width;
    if(self.alignStyle == ImageTiltleButonStyleVertical)
    {
        self.frame = CGRectMake(0,0, MAX(w1, w2)+self.titleEdgeInsets.left+self.titleEdgeInsets.right, self.imageView.image.size.height+self.titleLabel.frame.size.height+_contentGap+self.titleEdgeInsets.top+self.titleEdgeInsets.bottom);
    }
    else
    {
        self.frame = CGRectMake(0,0, w1+w2+self.titleEdgeInsets.left+self.titleEdgeInsets.right, MAX(self.imageView.frame.size.height,self.titleLabel.frame.size.height)+self.titleEdgeInsets.top+self.titleEdgeInsets.bottom);
    }
    self.center = center;
}

-(void)setContentGap:(CGFloat)contentGap
{
    _contentGap = contentGap;
    [self setNeedsLayout];
}


-(void)setAlignStyle:(ImageTiltleButonStyle)alignStyle
{
    if(_alignStyle != alignStyle)
    {
        _alignStyle = alignStyle;
        [self setNeedsLayout];
    }
}

-(void)setTitleEdgeInsets:(UIEdgeInsets)titleEdgeInsets
{
    [super setTitleEdgeInsets:titleEdgeInsets];
    [self setNeedsLayout];
}


@end
